package steveshrader.budget.client.widgets;

import steveshrader.budget.shared.BudgetRequestFactory;
import steveshrader.budget.shared.BudgetUserProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

/**
 * The top panel, the header
 */
public class Header extends Composite {

  interface Binder extends UiBinder<Widget, Header> { }
  private static final Binder binder = GWT.create(Binder.class);

  @UiField Anchor signOutLink;
  @UiField InlineLabel userNickname;
  @UiField Hidden userId;
  @UiField InlineLabel quote;

  public Header(BudgetRequestFactory requestFactory) {
    initWidget(binder.createAndBindUi(this));
    
    requestFactory.budgetServiceRequest().getBudgetUser().fire(
        	new Receiver<BudgetUserProxy>() {
        		@Override
                public void onSuccess(BudgetUserProxy response) {
        			userNickname.setText(response.getNickname());
        			userId.setDefaultValue(response.getUserId());
                }
            });
    
    requestFactory.budgetServiceRequest().getLogoutUrl().fire(
        	new Receiver<String>() {
        		@Override
                public void onSuccess(String response) {
        			signOutLink.setHref(response);
                }
            });
    
    requestFactory.budgetServiceRequest().getQuote().fire(
        	new Receiver<String>() {
        		@Override
                public void onSuccess(String response) {
        			if(response != null && response.length() > 0) {
        				quote.setText(response);
        			}
                }
            });
  }
}
